package com.weichen.studentgameexchange.game.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weichen.studentgameexchange.common.common.Result;
import com.weichen.studentgameexchange.game.entity.Game;
import com.weichen.studentgameexchange.game.entity.vo.AddGameVo;
import com.weichen.studentgameexchange.game.service.IGameService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-01
 */
@RestController
@RequestMapping("/game/game")
public class GameController {

    @Autowired
    IGameService gameService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

/**
 * TODO 用户-游戏--首页
 */
    /**
     * 用户查看所有游戏信息
     *
     * @return
     */
    @Operation(summary = "用户查看所有游戏信息接口")
    @GetMapping("/user/selectAll")
    public Result userSelectAll() {
        String json = stringRedisTemplate.opsForValue().get("Json");
        if (StrUtil.isEmpty(json)) {
            List<Game> gameList = gameService.list();
            String s = JSON.toJSONString(gameList);
            stringRedisTemplate.opsForValue().set("Json",s,1,TimeUnit.DAYS);
            return Result.success(gameList);
        }
        List<Game> games = JSON.parseObject(json, new TypeReference<List<Game>>() {
        });
        return Result.success(games);
    }

    /**
     * 用户查看单个游戏信息
     *
     * @param gameId
     * @return
     */
    @Operation(summary = "用户查看单个游戏信息接口")
    @GetMapping("/user/selectOne/{gameId}")
    public Result userSelectOne(@PathVariable("gameId") Long gameId) {
        return gameService.userSelectOne(gameId);
    }

    /**
     * 根据gameName条件分页查看游戏类型
     *
     * @param gameName
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "根据gameName条件分页查看游戏类型")
    @GetMapping("/user/page")
    public Result userPage(@RequestParam String gameName,
                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "size", defaultValue = "5") Integer size) {
        return gameService.userPage(gameName, page, size);
    }

    /**
     * 根据gameName条件分页查看游戏分类
     *
     * @param gameName
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "根据gameName条件分页查看游戏分类")
    @GetMapping("/user/sort/page")
    public Result userSortPage(@RequestParam String gameName,
                               @RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "size", defaultValue = "5") Integer size) {
        return gameService.userSortPage(gameName, page, size);
    }


/**
 * TODO 管理员-游戏--游戏管理
 */
    /**
     * 管理员新增游戏
     *
     * @param addGameVo
     * @return
     */
    @Operation(summary = "管理员新增游戏接口")
    @PostMapping("/admin/add")
    public Result adminAdd(@RequestBody AddGameVo addGameVo) {
        return gameService.adminAdd(addGameVo);
    }

    /**
     * 管理员分页查询游戏
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "管理员分页查询游戏信息接口")
    @GetMapping("/admin/page")
    public Result adminPage(@RequestParam("keyword") String keyword,
                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Game> gamePage = gameService.adminPage(keyword, page, size);
        return Result.success(gamePage);
    }

    /**
     * 管理员查看单个游戏信息
     *
     * @param gameId
     * @return
     */
    @Operation(summary = "管理员查看单个游戏信息接口")
    @GetMapping("/admin/selectOne/{gameId}")
    public Result adminSelectOne(@PathVariable("gameId") Long gameId) {
        Game game = gameService.getById(gameId);
        return Result.success(game);
    }

    /**
     * 管理员修改游戏信息
     *
     * @param game
     * @return
     */
    @Operation(summary = "管理员修改游戏信息接口")
    @PutMapping("/admin/update")
    public Result adminUpdate(@RequestBody Game game) {
        game.setUpdateTime(LocalDateTime.now());
        gameService.updateById(game);
        return Result.success();
    }

    /**
     * 管理员删除单个游戏
     *
     * @param gameId
     * @return
     */
    @Operation(summary = "管理员删除单个游戏接口")
    @DeleteMapping("/admin/delete/{gameId}")
    public Result adminDeleteOne(@PathVariable("gameId") Long gameId) {
        return Result.success(gameService.adminDeleteOne(gameId));
    }

    /**
     * 管理员批量删除多个游戏
     *
     * @return
     */
    @Operation(summary = "管理员批量删除多个游戏接口")
    @DeleteMapping("/admin/delete/batch")
    public Result adminDeleteBatch(@RequestBody List<Long> gameIds) {
        for (Long gameId : gameIds)
            adminDeleteOne(gameId);
        return Result.success();
    }

    /**
     * 管理员查看所有游戏信息->给游戏版本用，下拉框显示所有游戏
     *
     * @return
     */
    @Operation(summary = "管理员查看所有游戏信息接口")
    @GetMapping("/admin/selectAll")
    public Result adminSelectAll() {
        return Result.success(gameService.list());
    }

    /**
     * 管理员查看所有游戏信息->给开发商,游戏类型,攻略用，下拉框显示所有游戏
     *
     * @return
     */
    @Operation(summary = "管理员查看所有游戏信息接口")
    @GetMapping("/admin/selectAllDeveloper")
    public Result selectAllDeveloper() {
        return gameService.selectAllDeveloper();
    }
}
